package cn.hl.hm.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.hl.hm.dao.BackStageMenusMapper;
import cn.hl.hm.dao.BackStageRoleMenuMapper;
import cn.hl.hm.dao.BackStageRolesMapper;
import cn.hl.hm.dao.BackStageUserRoleMapper;
import cn.hl.hm.dao.TBackstageUserEntityMapper;
import cn.hl.hm.entity.backstage.BackStageMenus;
import cn.hl.hm.entity.backstage.BackStageRoleMenu;
import cn.hl.hm.entity.backstage.BackStageRoles;
import cn.hl.hm.entity.backstage.BackStageUserRole;
import cn.hl.hm.entity.backstage.TBackstageUserEntity;
import cn.hl.hm.service.BackstageAuthService;

@Component
public class BackstageAuthServiceImpl implements BackstageAuthService {
	@Autowired
	public BackStageMenusMapper menusMapper;

	@Autowired
	public BackStageRolesMapper rolesMapper;

	@Autowired
	public BackStageRoleMenuMapper roleMenuMapper;

	@Autowired
	public BackStageUserRoleMapper userRoleMapper;

	@Autowired
	TBackstageUserEntityMapper tUserEntityMapper;
	
	
	
	
	//根据用户名查id
	@Override
	public BackStageRoles selectByroleName(BackStageRoles roleName) {
		// TODO Auto-generated method stub
		return rolesMapper.selectByroleName(roleName);
	}
	
	//添加角色权限 
	@Override
	public int insertRoleMenu(BackStageRoleMenu role) {
		// TODO Auto-generated method stub
		return rolesMapper.insertRoleMenu(role);
	}
	
	//修改角色权限前先删除源数据 
	@Override
	public int deleteByRoleId(Integer id) {
		// TODO Auto-generated method stub
		return rolesMapper.deleteByRoleId(id);
	}
	
	
	@Override
	public List<BackStageRoleMenu> selectInfo(BackStageRoleMenu record) {
		// TODO Auto-generated method stub
		return roleMenuMapper.selectInfo(record);
	}
	
	
	//------------------------------------------------------------------
	public List<BackStageMenus> selectUserAuths(Integer userId) {
		return menusMapper.selectUserAuths(userId);

	}

	@Override
	public List<BackStageRoles> selectRoles() {
		return rolesMapper.selectAllRoles();
	}

	@Override
	public List<BackStageMenus> selectMenus() {
		return menusMapper.selectAllMenus();
	}

	@Override
	public List<BackStageRoles> selectUserRoles(Integer userId) {
		return rolesMapper.selectUserRoles(userId);
	}

	@Override
	public int addRoles(BackStageRoles role) {
		if (role == null || StringUtils.isBlank(role.getRoleName())) {
			return 0;
		}
		List<BackStageRoles> selectAllRoles = rolesMapper.selectAllRoles();
		if (selectAllRoles != null && selectAllRoles.size() > 0) {
			for (BackStageRoles backStageRoles : selectAllRoles) {
				String roleName = backStageRoles.getRoleName();
				if (role.getRoleName().equals(roleName)) {
					return 0;
				}
			}
		}
		role.setCreateTime(new Date());
		return rolesMapper.insertSelective(role);
	}

	@Override
	public int addMenus(BackStageMenus menu) {
		if (menu == null || StringUtils.isBlank(menu.getMenuName())) {
			return 0;
		}
		List<BackStageMenus> selectAllMenus = menusMapper.selectAllMenus();
		if (selectAllMenus != null && selectAllMenus.size() > 0) {
			for (BackStageMenus backStageMenus : selectAllMenus) {
				if (backStageMenus.getMenuName().equals(menu.getMenuName())) {
					return 0;
				}
			}
		}
		menu.setCreateTime(new Date());
		return menusMapper.insert(menu);
	}

	@Override
	public int addUserRoles(Integer userId, Integer roleId) {
		TBackstageUserEntity user = tUserEntityMapper.selectByPrimaryKey(userId);
		BackStageRoles role = rolesMapper.selectByPrimaryKey(roleId);
		if (user == null || role == null) {
			return 0;
		}
		List<BackStageUserRole> selectByIds = userRoleMapper.selectByIds(userId, roleId);
		if(selectByIds!=null&&selectByIds.size()>0){
			return 0;
		}
		BackStageUserRole record = new BackStageUserRole();
		record.setUserId(userId);
		record.setRoleId(roleId);
		record.setCreateTime(new Date());
		return userRoleMapper.insertSelective(record);
	}

	@Override
	public int addRoleMenu(Integer menuId, Integer roleId) {
		BackStageRoles role = rolesMapper.selectByPrimaryKey(roleId);
		BackStageMenus menu = menusMapper.selectByPrimaryKey(menuId);
		if (menu == null || role == null) {
			return 0;
		}
		List<BackStageRoleMenu> selectByIds = roleMenuMapper.selectByIds(menuId, roleId);
		if(selectByIds!=null&&selectByIds.size()>0){
			return 0;
		}
		BackStageRoleMenu record = new BackStageRoleMenu();
		record.setMenuId(menuId);
		record.setRoleId(roleId);
		record.setCreateTime(new Date());
		return roleMenuMapper.insertSelective(record);
	}

	@Override
	public int deleteRoleMenu(Integer id) {

		return roleMenuMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteUserRoles(Integer id) {
		return userRoleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateRoleInfo(BackStageRoles role) {
		if (role == null || role.getId() == null||StringUtils.isBlank(role.getRoleName())) {
			return 0;
		}
		return rolesMapper.updateByPrimaryKey(role);
	}

	@Override
	public int updateMenuInfo(BackStageMenus menu) {
		if (menu == null || menu.getId() == null||StringUtils.isBlank(menu.getMenuName())) {
			return 0;
		}
		return menusMapper.updateByPrimaryKey(menu);

	}

	@Override
	public List<BackStageRoleMenu> selectRoleMenus() {
		return roleMenuMapper.selectAllRoleMenus();
	}

	@Override
	public List<BackStageUserRole> selectUserRoles() {
		return userRoleMapper.selectAllUserRoles();
	}




	
	

}
